home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / comm2 / termsorc.lha / Extras / Source / term-source.lha / termSerial.c < prev    next >
C/C++ Source or Header  |  1995-09-26  |  40KB  |  2,112 lines

  1. /*
  2. **    termSerial.c
  3. **
  4. **    Serial driver support routines
  5. **
  6. **    Copyright © 1990-1995 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. **
  9. **    :ts=4
  10. */
  11.  
  12. #include "termGlobal.h"
  13.  
  14.     /* Local copy of serial driver name and unit number. */
  15.  
  16. STATIC UBYTE __far    SerialDevice[40];
  17. STATIC LONG            UnitNumber = -1;
  18.  
  19. STATIC BOOLEAN        SerialLocked;
  20.  
  21.     /* SetFlags():
  22.      *
  23.      *    Set the contents of a serial device request according
  24.      *    to the current configuration settings.
  25.      */
  26.  
  27. STATIC BYTE __regargs
  28. SetFlags(VOID)
  29. {
  30.     return(SetBothSerialAttributes(
  31.         SERA_Baud,            Config -> SerialConfig -> BaudRate,
  32.         SERA_BreakTime,        Config -> SerialConfig -> BreakLength,
  33.         SERA_BitsPerChar,    Config -> SerialConfig -> BitsPerChar,
  34.         SERA_StopBits,        Config -> SerialConfig -> StopBits,
  35.         SERA_BufferSize,    Config -> SerialConfig -> SerialBufferSize,
  36.         SERA_Parity,        Config -> SerialConfig -> Parity,
  37.         SERA_Handshaking,    UseRTS_CTS ? Config -> SerialConfig -> HandshakingProtocol : HANDSHAKING_NONE,
  38.         SERA_HighSpeed,        Config -> SerialConfig -> HighSpeed,
  39.         SERA_Shared,        Config -> SerialConfig -> Shared,
  40.     TAG_DONE));
  41. }
  42.  
  43.     /* SendBreak():
  44.      *
  45.      *    Transmit a break signal.
  46.      */
  47.  
  48. VOID
  49. SendBreak()
  50. {
  51.     BYTE OldStatus = Status;
  52.  
  53.     Status = STATUS_BREAKING;
  54.  
  55.     DoSerialCmd(SDCMD_BREAK);
  56.  
  57.     Status = OldStatus;
  58. }
  59.  
  60.     /* HangUp():
  61.      *
  62.      *    Hang up the line.
  63.      */
  64.  
  65. VOID
  66. HangUp()
  67. {
  68.     BYTE OldStatus = Status;
  69.  
  70.     StopSerialWrite();
  71.  
  72.     Status = STATUS_HANGUP;
  73.  
  74.         /* Are we to drop the DTR line
  75.          * before sending the hangup
  76.          * string?
  77.          */
  78.  
  79.     if(Config -> ModemConfig -> DropDTR && WriteRequest)
  80.     {
  81.         /* Let's be nice and try to transmit the
  82.          * `drop the line' command before
  83.          * trying to close and reopen the driver.
  84.          */
  85.  
  86.         WriteRequest -> IOSer . io_Command    = SIOCMD_SETCTRLLINES;
  87.         WriteRequest -> IOSer . io_Offset    = SIOB_DTRF;
  88.         WriteRequest -> IOSer . io_Length    = 0;
  89.  
  90.             /* Transmit the command. */
  91.  
  92.         if(!DoIO(WriteRequest))
  93.         {
  94.                 /* Wait a bit... */
  95.  
  96.             WaitTime(1,0);
  97.  
  98.                 /* Raise the line again. */
  99.  
  100.             WriteRequest -> IOSer . io_Command    = SIOCMD_SETCTRLLINES;
  101.             WriteRequest -> IOSer . io_Offset    = SIOB_DTRF;
  102.             WriteRequest -> IOSer . io_Length    = SIOB_DTRF;
  103.  
  104.             DoIO(WriteRequest);
  105.         }
  106.         else
  107.         {
  108.                 /* Do it the standard way: close and reopen
  109.                  * the serial driver (the serial.device is
  110.                  * supposed to drop the DTR line when closed).
  111.                  */
  112.  
  113.             if(!DropDTR())
  114.             {
  115.                 if(!MyEasyRequest(Window,LocaleString(MSG_TERMMAIN_FAILED_TO_REOPEN_UNIT_TXT),LocaleString(MSG_TERMMAIN_IGNORE_QUIT_TXT),Config -> SerialConfig -> SerialDevice,Config -> SerialConfig -> UnitNumber))
  116.                     MainTerminated = TRUE;
  117.             }
  118.         }
  119.     }
  120.  
  121.         /* Transmit the hangup command. */
  122.  
  123.     if(Config -> ModemConfig -> ModemHangup[0])
  124.         SerialCommand(Config -> ModemConfig -> ModemHangup);
  125.  
  126.         /* Reset to old status. */
  127.  
  128.     Status = OldStatus;
  129. }
  130.  
  131.     /* DropDTR():
  132.      *
  133.      *    Drop the data terminal ready signal (i.e. close, wait a bit
  134.      *    and then reopen the serial drive).
  135.      */
  136.  
  137. BYTE
  138. DropDTR()
  139. {
  140.         /* Finish all serial read activity. */
  141.  
  142.     ClearSerial();
  143.  
  144.         /* Do we have any channels to work with? */
  145.  
  146.     if(ReadRequest && WriteRequest)
  147.     {
  148.             /* Close the device. */
  149.  
  150.         CloseDevice(ReadRequest);
  151.  
  152.             /* Wait a bit. */
  153.  
  154.         WaitTime(1,0);
  155.  
  156.             /* Reopen the driver. */
  157.  
  158.         if(!OpenDevice(Config -> SerialConfig -> SerialDevice,Config -> SerialConfig -> UnitNumber,ReadRequest,0))
  159.         {
  160.             struct MsgPort *WritePort = WriteRequest -> IOSer . io_Message . mn_ReplyPort;
  161.  
  162.             ResetSerialRead();
  163.             ResetSerialWrite();
  164.  
  165.                 /* Fill in the rest. */
  166.  
  167.             CopyMem(ReadRequest,WriteRequest,sizeof(struct IOExtSer));
  168.  
  169.             WriteRequest -> IOSer . io_Message . mn_ReplyPort = WritePort;
  170.  
  171.             SetFlags();
  172.  
  173.             DoSerialCmd(SDCMD_SETPARAMS);
  174.  
  175.                 /* Restart read activity. */
  176.  
  177.             RestartSerial();
  178.  
  179.             return(TRUE);
  180.         }
  181.         else
  182.             DeleteSerial();
  183.     }
  184.     else
  185.     {
  186.         DeleteSerial();
  187.  
  188.         return(TRUE);
  189.     }
  190.  
  191.     return(FALSE);
  192. }
  193.  
  194.     /* CopyWriteFlags():
  195.      *
  196.      *    Update configuration with serial settings.
  197.      */
  198.  
  199. VOID
  200. CopyWriteFlags()
  201. {
  202.     ULONG    Baud,
  203.             BreakTime,
  204.             BitsPerChar,
  205.             StopBits,
  206.             BufferSize,
  207.             Parity,
  208.             Handshaking,
  209.             HighSpeed,
  210.             Shared;
  211.  
  212.     GetSerialWriteAttributes(
  213.         SERA_Baud,            &Baud,
  214.         SERA_BreakTime,        &BreakTime,
  215.         SERA_BitsPerChar,    &BitsPerChar,
  216.         SERA_StopBits,        &StopBits,
  217.         SERA_BufferSize,    &BufferSize,
  218.         SERA_Parity,        &Parity,
  219.         SERA_Handshaking,    &Handshaking,
  220.         SERA_HighSpeed,        &HighSpeed,
  221.         SERA_Shared,        &Shared,
  222.     TAG_DONE);
  223.  
  224.     Config -> SerialConfig -> BaudRate                = Baud;
  225.     Config -> SerialConfig -> BreakLength            = BreakTime;
  226.     Config -> SerialConfig -> BitsPerChar            = BitsPerChar;
  227.     Config -> SerialConfig -> StopBits                = StopBits;
  228.     Config -> SerialConfig -> SerialBufferSize        = BufferSize;
  229.     Config -> SerialConfig -> Parity                = Parity;
  230.     Config -> SerialConfig -> HandshakingProtocol    = Handshaking;
  231.     Config -> SerialConfig -> HighSpeed                = HighSpeed;
  232.     Config -> SerialConfig -> Shared                = Shared;
  233. }
  234.  
  235.     /* CallMenu(STRPTR Name,ULONG Code):
  236.      *
  237.      *    Call a menu function either through the name of the corresponding
  238.      *    menu item or a menu number.
  239.      */
  240.  
  241. STATIC VOID __inline
  242. CallMenu(STRPTR Name,ULONG Code)
  243. {
  244.     WORD MenuNum = -1,Item = 0,Sub = 0,i;
  245.  
  246.         /* Are we to look for a name? */
  247.  
  248.     if(Name)
  249.     {
  250.         WORD Len = strlen(Name);
  251.  
  252.             /* Scan the menu list... */
  253.  
  254.         for(i = 0 ; TermMenu[i] . nm_Type != NM_END ; i++)
  255.         {
  256.             switch(TermMenu[i] . nm_Type)
  257.             {
  258.                 case NM_TITLE:
  259.  
  260.                     MenuNum++;
  261.                     Item = Sub = 0;
  262.                     break;
  263.  
  264.                 case NM_ITEM:
  265.  
  266.                     Sub = 0;
  267.                     break;
  268.             }
  269.  
  270.                 /* Did we get a valid name string? */
  271.  
  272.             if(TermMenu[i] . nm_Label != NM_BARLABEL)
  273.             {
  274.                     /* Does the name match our template? */
  275.  
  276.                 if(!Strnicmp(TermMenu[i] . nm_Label,Name,Len))
  277.                 {
  278.                     struct MenuItem *MenuItem = ItemAddress(Menu,FULLMENUNUM(MenuNum,Item,Sub));
  279.  
  280.                     if(MenuItem && (MenuItem -> Flags & ITEMENABLED))
  281.                         HandleMenuCode((ULONG)TermMenu[i] . nm_UserData,NULL);
  282.  
  283.                     break;
  284.                 }
  285.             }
  286.  
  287.             switch(TermMenu[i] . nm_Type)
  288.             {
  289.                 case NM_ITEM:
  290.  
  291.                     Item++;
  292.                     break;
  293.  
  294.                 case NM_SUB:
  295.  
  296.                     Sub++;
  297.                     break;
  298.             }
  299.         }
  300.     }
  301.     else
  302.     {
  303.         WORD    TheMenu    =  Code % 100,
  304.                 TheItem    = (Code / 100) % 100,
  305.                 TheSub    =  Code / 10000;
  306.  
  307.             /* Scan the menu list... */
  308.  
  309.         for(i = 0 ; TermMenu[i] . nm_Type != NM_END ; i++)
  310.         {
  311.             switch(TermMenu[i] . nm_Type)
  312.             {
  313.                 case NM_TITLE:
  314.  
  315.                     MenuNum++;
  316.                     Item = Sub = 0;
  317.                     break;
  318.  
  319.                 case NM_ITEM:
  320.  
  321.                     Sub = 0;
  322.                     break;
  323.             }
  324.  
  325.                 /* Is it the menu number we want? */
  326.  
  327.             if(TheMenu == MenuNum && TheItem == Item && TheSub == Sub)
  328.             {
  329.                 if(TermMenu[i] . nm_Label != NM_BARLABEL)
  330.                 {
  331.                     struct MenuItem *MenuItem = ItemAddress(Menu,FULLMENUNUM(MenuNum,Item,Sub));
  332.  
  333.                     if(MenuItem && (MenuItem -> Flags & ITEMENABLED))
  334.                         HandleMenuCode((ULONG)TermMenu[i] . nm_UserData,NULL);
  335.                 }
  336.  
  337.                 break;
  338.             }
  339.  
  340.             switch(TermMenu[i] . nm_Type)
  341.             {
  342.                 case NM_ITEM:
  343.  
  344.                     Item++;
  345.                     break;
  346.  
  347.                 case NM_SUB:
  348.  
  349.                     Sub++;
  350.                     break;
  351.             }
  352.         }
  353.     }
  354. }
  355.  
  356.     /* SerialCommand(STRPTR String):
  357.      *
  358.      *    Send a command string to the serial line and
  359.      *    interprete the control sequences.
  360.      */
  361.  
  362. VOID __regargs
  363. SerialCommand(STRPTR String)
  364. {
  365.     BYTE    (*  SendLineLocal)(register STRPTR,register LONG);
  366.  
  367.     LONG    Count = 0,i,Len = strlen(String);
  368.  
  369.     BYTE    GotControl    = FALSE,
  370.             GotEscape    = FALSE;
  371.  
  372.     SendLineLocal = SendLine;
  373.  
  374.         /* Scan the string. */
  375.  
  376.     for(i = 0 ; i < Len ; i++)
  377.     {
  378.             /* We are looking for plain characters
  379.              * and the control ('\') and escape
  380.              * ('^') characters.
  381.              */
  382.  
  383.         if(!GotControl && !GotEscape)
  384.         {
  385.                 /* Got a control character,
  386.                  * the next byte will probably be
  387.                  * a command sequence.
  388.                  */
  389.  
  390.             if(String[i] == '\\')
  391.             {
  392.                 GotControl = TRUE;
  393.                 continue;
  394.             }
  395.  
  396.                 /* Got an escape character,
  397.                  * the next byte will be some
  398.                  * kind of control character
  399.                  * (such as XON, XOF, bell, etc.).
  400.                  */
  401.  
  402.             if(String[i] == '^')
  403.             {
  404.                 GotEscape = TRUE;
  405.                 continue;
  406.             }
  407.  
  408.                 /* This tells us to wait another
  409.                  * second before continuing with
  410.                  * the scanning.
  411.                  */
  412.  
  413.             if(String[i] == '~')
  414.             {
  415.                 if(Count)
  416.                 {
  417.                     (*SendLineLocal)(SharedBuffer,Count);
  418.  
  419.                     Count = 0;
  420.                 }
  421.  
  422.                 WaitTime(0,MILLION / 2);
  423.  
  424.                 HandleSerial();
  425.  
  426.                 continue;
  427.             }
  428.  
  429.                 /* Stuff the character into the
  430.                  * buffer.
  431.                  */
  432.  
  433.             SharedBuffer[Count++] = String[i];
  434.         }
  435.         else
  436.         {
  437.                 /* Convert the character to a control
  438.                  * style character (^C, etc.).
  439.                  */
  440.  
  441.             if(GotEscape)
  442.             {
  443.                 if(ToUpper(String[i]) >= 'A' && ToUpper(String[i]) <= '_')
  444.                     SharedBuffer[Count++] = ToUpper(String[i]) - '@';
  445.                 else
  446.                     SharedBuffer[Count++] = String[i];
  447.  
  448.                 GotEscape = FALSE;
  449.             }
  450.  
  451.                 /* The next character represents a command. */
  452.  
  453.             if(GotControl)
  454.             {
  455.                 switch(ToUpper(String[i]))
  456.                 {
  457.                         /* Fall back to default send mode. */
  458.  
  459.                     case '0':
  460.  
  461.                         if(Count)
  462.                         {
  463.                             (*SendLineLocal)(SharedBuffer,Count);
  464.  
  465.                             Count = 0;
  466.                         }
  467.  
  468.                         SendLineLocal = SendLine;
  469.                         break;
  470.  
  471.                         /* Select `direct' send mode. */
  472.  
  473.                     case '1':
  474.  
  475.                         if(Count)
  476.                         {
  477.                             (*SendLineLocal)(SharedBuffer,Count);
  478.  
  479.                             Count = 0;
  480.                         }
  481.  
  482.                         SendLineLocal = SendLineSimple;
  483.                         break;
  484.  
  485.                         /* Select `echo' send mode. */
  486.  
  487.                     case '2':
  488.  
  489.                         if(Count)
  490.                         {
  491.                             (*SendLineLocal)(SharedBuffer,Count);
  492.  
  493.                             Count = 0;
  494.                         }
  495.  
  496.                         if(Config -> ClipConfig -> SendTimeout)
  497.                             SendLineLocal = SendLineEcho;
  498.                         else
  499.                             SendLineLocal = SendLineSimple;
  500.  
  501.                         break;
  502.  
  503.                         /* Select `any echo' send mode. */
  504.  
  505.                     case '3':
  506.  
  507.                         if(Count)
  508.                         {
  509.                             (*SendLineLocal)(SharedBuffer,Count);
  510.  
  511.                             Count = 0;
  512.                         }
  513.  
  514.                         if(Config -> ClipConfig -> SendTimeout)
  515.                             SendLineLocal = SendLineAnyEcho;
  516.                         else
  517.                             SendLineLocal = SendLineSimple;
  518.  
  519.                         break;
  520.  
  521.                         /* Select `prompt' send mode. */
  522.  
  523.                     case '4':
  524.  
  525.                         if(Count)
  526.                         {
  527.                             (*SendLineLocal)(SharedBuffer,Count);
  528.  
  529.                             Count = 0;
  530.                         }
  531.  
  532.                         if(Config -> ClipConfig -> SendTimeout)
  533.                             SendLineLocal = SendLinePrompt;
  534.                         else
  535.                             SendLineLocal = SendLineSimple;
  536.  
  537.                         break;
  538.  
  539.                         /* Select `delay' send mode. */
  540.  
  541.                     case '5':
  542.  
  543.                         if(Count)
  544.                         {
  545.                             (*SendLineLocal)(SharedBuffer,Count);
  546.  
  547.                             Count = 0;
  548.                         }
  549.  
  550.                         if(Config -> ClipConfig -> LineDelay || Config -> ClipConfig -> CharDelay)
  551.                             SendLineLocal = SendLineDelay;
  552.                         else
  553.                             SendLineLocal = SendLineSimple;
  554.  
  555.                         break;
  556.  
  557.                         /* Select `keyboard' send mode. */
  558.  
  559.                     case '6':
  560.  
  561.                         if(Count)
  562.                         {
  563.                             (*SendLineLocal)(SharedBuffer,Count);
  564.  
  565.                             Count = 0;
  566.                         }
  567.  
  568.                         SendLineLocal = SendLineKeyDelay;
  569.                         break;
  570.  
  571.                         /* Translate code. */
  572.  
  573.                     case '*':
  574.  
  575.                         i++;
  576.  
  577.                         while(i < Len && String[i] == ' ')
  578.                             i++;
  579.  
  580.                         if(i < Len)
  581.                         {
  582.                             UBYTE DummyBuffer[5],j = 0,Char;
  583.  
  584.                             if(String[i] >= '0' && String[i] <= '9')
  585.                             {
  586.                                 while(j < 3 && i < Len)
  587.                                 {
  588.                                     Char = String[i++];
  589.  
  590.                                     if(Char >= '0' && Char <= '9')
  591.                                         DummyBuffer[j++] = Char;
  592.                                     else
  593.                                     {
  594.                                         i--;
  595.  
  596.                                         break;
  597.                                     }
  598.                                 }
  599.                             }
  600.                             else
  601.                             {
  602.                                 while(j < 4 && i < Len)
  603.                                 {
  604.                                     Char = ToLower(String[i++]);
  605.  
  606.                                     if((Char >= '0' && Char <= '9') || (Char >= 'a' && Char <= 'z'))
  607.                                         DummyBuffer[j++] = Char;
  608.                                     else
  609.                                     {
  610.                                         i--;
  611.  
  612.                                         break;
  613.                                     }
  614.                                 }
  615.                             }
  616.  
  617.                             DummyBuffer[j] = 0;
  618.  
  619.                             SharedBuffer[Count++] = NameToCode(DummyBuffer);
  620.                         }
  621.  
  622.                         i--;
  623.  
  624.                         break;
  625.  
  626.                         /* Execute an AmigaDOS command. */
  627.  
  628.                     case 'D':
  629.  
  630.                         if(!InRexx)
  631.                         {
  632.                             if(!WeAreBlocking)
  633.                             {
  634.                                 BlockWindows();
  635.  
  636.                                 SendAmigaDOSCommand(&String[i + 1]);
  637.  
  638.                                 ReleaseWindows();
  639.                             }
  640.                             else
  641.                                 SendAmigaDOSCommand(&String[i + 1]);
  642.                         }
  643.  
  644.                         return;
  645.  
  646.                         /* Execute an ARexx command. */
  647.  
  648.                     case 'A':
  649.  
  650.                         if(!InRexx)
  651.                         {
  652.                             if(!WeAreBlocking)
  653.                             {
  654.                                 BlockWindows();
  655.  
  656.                                 SendARexxCommand(&String[i + 1],TRUE);
  657.  
  658.                                 ReleaseWindows();
  659.                             }
  660.                             else
  661.                                 SendARexxCommand(&String[i + 1],TRUE);
  662.                         }
  663.  
  664.                         return;
  665.  
  666.                         /* Add the control character ('\'). */
  667.  
  668.                     case '\\':
  669.  
  670.                         SharedBuffer[Count++] = '\\';
  671.                         break;
  672.  
  673.                         /* This is a backspace. */
  674.  
  675.                     case 'B':
  676.  
  677.                         SharedBuffer[Count++] = '\b';
  678.                         break;
  679.  
  680.                         /* This is a form feed. */
  681.  
  682.                     case 'F':
  683.  
  684.                         SharedBuffer[Count++] = '\f';
  685.                         break;
  686.  
  687.                         /* This is a line feed. */
  688.  
  689.                     case 'N':
  690.  
  691.                         SharedBuffer[Count++] = '\n';
  692.                         break;
  693.  
  694.                         /* Send the current password. */
  695.  
  696.                     case 'P':
  697.  
  698.                         if(Password[0])
  699.                         {
  700.                             if(Count)
  701.                             {
  702.                                 (*SendLineLocal)(SharedBuffer,Count);
  703.  
  704.                                 Count = 0;
  705.                             }
  706.  
  707.                             (*SendLineLocal)(Password,strlen(Password));
  708.                         }
  709.  
  710.                         break;
  711.  
  712.                         /* This is a carriage return. */
  713.  
  714.                     case 'R':
  715.  
  716.                         SharedBuffer[Count++] = '\r';
  717.                         break;
  718.  
  719.                         /* This is a tab. */
  720.  
  721.                     case 'T':
  722.  
  723.                         SharedBuffer[Count++] = '\t';
  724.                         break;
  725.  
  726.                         /* Send the current user name. */
  727.  
  728.                     case 'U':
  729.  
  730.                         if(UserName[0])
  731.                         {
  732.                             if(Count)
  733.                             {
  734.                                 (*SendLineLocal)(SharedBuffer,Count);
  735.  
  736.                                 Count = 0;
  737.                             }
  738.  
  739.                             (*SendLineLocal)(UserName,strlen(UserName));
  740.                         }
  741.  
  742.                         break;
  743.  
  744.                         /* Send a break across the serial line. */
  745.  
  746.                     case 'X':
  747.  
  748.                         if(Count)
  749.                         {
  750.                             (*SendLineLocal)(SharedBuffer,Count);
  751.  
  752.                             Count = 0;
  753.                         }
  754.  
  755.                         SendBreak();
  756.  
  757.                         break;
  758.  
  759.                         /* Feed the contents of the
  760.                          * clipboard into the input
  761.                          * stream.
  762.                          */
  763.  
  764.                     case 'I':
  765.  
  766.                         if(Count)
  767.                             (*SendLineLocal)(SharedBuffer,Count);
  768.  
  769.                         Count = LoadClip(SharedBuffer,256);
  770.  
  771.                         break;
  772.  
  773.                         /* Send a string to the clipboard. */
  774.  
  775.                     case 'G':
  776.  
  777.                         if(String[i + 1])
  778.                             SaveClip(&String[i + 1],strlen(&String[i + 1]));
  779.  
  780.                         return;
  781.  
  782.                         /* Send a string to the clipboard. */
  783.  
  784.                     case 'H':
  785.  
  786.                         if(String[i + 1])
  787.                             AddClip(&String[i + 1],strlen(&String[i + 1]));
  788.  
  789.                         return;
  790.  
  791.                         /* Produce the escape character. */
  792.  
  793.                     case 'E':
  794.  
  795.                         SharedBuffer[Count++] = ESC;
  796.                         break;
  797.  
  798.                         /* Call a menu item. */
  799.  
  800.                     case 'C':
  801.  
  802.                         i++;
  803.  
  804.                             /* Scan for a menu number or
  805.                              * a single quote...
  806.                              */
  807.  
  808.                         while(i < Len)
  809.                         {
  810.                             if(String[i] >= '0' && String[i] <= '9')
  811.                                 break;
  812.  
  813.                             if(String[i] == '\'')
  814.                                 break;
  815.  
  816.                             if(String[i] != ' ')
  817.                                 break;
  818.  
  819.                             i++;
  820.                         }
  821.  
  822.                         if(i < Len)
  823.                         {
  824.                             UBYTE DummyBuffer[256];
  825.  
  826.                                 /* Did we get a quote? */
  827.  
  828.                             if(String[i] == '\'')
  829.                             {
  830.                                 LONG Start = ++i;
  831.  
  832.                                 if(String[Start])
  833.                                 {
  834.                                     WORD Length;
  835.  
  836.                                     while(i < Len)
  837.                                     {
  838.                                         if(String[i] != '\'')
  839.                                             i++;
  840.                                         else
  841.                                             break;
  842.                                     }
  843.  
  844.                                     if(String[i] == '\'')
  845.                                         Length = i - Start;
  846.                                     else
  847.                                         Length = i - Start + 1;
  848.  
  849.                                     memcpy(DummyBuffer,&String[Start],Length);
  850.  
  851.                                     DummyBuffer[Length] = 0;
  852.  
  853.                                     CallMenu(DummyBuffer,0);
  854.                                 }
  855.                             }
  856.                             else
  857.                             {
  858.                                 if(String[i] >= '0' && String[i] <= '9')
  859.                                 {
  860.                                     LONG Start = i,Length;
  861.  
  862.                                     while(i < Len)
  863.                                     {
  864.                                         if(String[i] >= '0' && String[i] <= '9')
  865.                                             i++;
  866.                                         else
  867.                                             break;
  868.                                     }
  869.  
  870.                                     if(i == Start)
  871.                                         Length = 1;
  872.                                     else
  873.                                         Length = i - Start;
  874.  
  875.                                     memcpy(DummyBuffer,&String[Start],Length);
  876.  
  877.                                     DummyBuffer[Length] = 0;
  878.  
  879.                                     CallMenu(NULL,Atol(DummyBuffer));
  880.                                 }
  881.                             }
  882.                         }
  883.  
  884.                         break;
  885.  
  886.                         /* Insert the current dialing mode suffix. */
  887.  
  888.                     case 'W':
  889.  
  890.                         if(Config -> ModemConfig -> DialMode == DIALMODE_PULSE)
  891.                             SharedBuffer[Count++] = 'P';
  892.                         else
  893.                             SharedBuffer[Count++] = 'T';
  894.  
  895.                         break;
  896.  
  897.                         /* Stuff the character into the buffer. */
  898.  
  899.                     default:
  900.  
  901.                         SharedBuffer[Count++] = String[i];
  902.                         break;
  903.                 }
  904.  
  905.                 GotControl = FALSE;
  906.             }
  907.         }
  908.  
  909.             /* If the buffer is full, release it. */
  910.  
  911.         if(Count == 256)
  912.         {
  913.             (*SendLineLocal)(SharedBuffer,Count);
  914.  
  915.             Count = 0;
  916.         }
  917.     }
  918.  
  919.     if(Count)
  920.         (*SendLineLocal)(SharedBuffer,Count);
  921. }
  922.  
  923.     /* SerWriteVerbatim(APTR Buffer,LONG Size,BOOLEAN Echo):
  924.      *
  925.      *    The `no fancy features' version of SerWrite().
  926.      */
  927.  
  928. VOID __regargs
  929. SerWriteVerbatim(APTR Buffer,LONG Size,BOOLEAN Echo)
  930. {
  931.     if(XProtocolBase && (TransferBits & XPRS_USERMON))
  932.         Size = XProtocolUserMon(XprIO,Buffer,Size,Size);
  933.  
  934.     if(Size > 0)
  935.     {
  936.         if(Echo)
  937.         {
  938.             if(Marking)
  939.                 DropMarker();
  940.  
  941.             StartSerialWrite(Buffer,Size);
  942.  
  943.             ConProcess(Buffer,Size);
  944.  
  945.             WaitSerialWrite();
  946.         }
  947.         else
  948.             DoSerialWrite(Buffer,Size);
  949.  
  950.         BytesOut += Size;
  951.     }
  952. }
  953.  
  954.     /* SerWrite(APTR Buffer,LONG Size):
  955.      *
  956.      *    Send a number of bytes across the serial line.
  957.      */
  958.  
  959. VOID __regargs
  960. SerWrite(APTR Buffer,LONG Size)
  961. {
  962.     if(Size < 0)
  963.         Size = strlen(Buffer);
  964.  
  965.     if(Size)
  966.     {
  967.         if(RememberInput)
  968.         {
  969.             RememberInputText(Buffer,Size);
  970.  
  971.             if(((UBYTE *)Buffer)[Size - 1] == '\r' && RecordingLine)
  972.             {
  973.                 RememberSpill();
  974.  
  975.                 RecordingLine = FALSE;
  976.  
  977.                 RememberOutput = TRUE;
  978.                 RememberInput = FALSE;
  979.  
  980.                 CheckItem(MEN_RECORD_LINE,FALSE);
  981.             }
  982.         }
  983.         else
  984.         {
  985.             if(RememberOutput)
  986.             {
  987.                 RememberInputText(Buffer,Size);
  988.  
  989.                 RememberSpill();
  990.             }
  991.         }
  992.  
  993.         if(WriteRequest)
  994.         {
  995.             STATIC UBYTE __far TranslateData[512];
  996.  
  997.                 /* xpr wants to see the data before it is
  998.                  * transferred.
  999.                  */
  1000.  
  1001.             if(XProtocolBase && (TransferBits & XPRS_USERMON))
  1002.             {
  1003.                 if(Size = XProtocolUserMon(XprIO,Buffer,Size,Size))
  1004.                 {
  1005.                     if(SendTable)
  1006.                     {
  1007.                         struct TranslationHandle Handle;
  1008.  
  1009.                             /* Set up for buffer translation. */
  1010.  
  1011.                         TranslateSetup(&Handle,Buffer,Size,TranslateData,512,SendTable);
  1012.  
  1013.                             /* Full or half duplex? */
  1014.  
  1015.                         if(Config -> SerialConfig -> Duplex == DUPLEX_FULL)
  1016.                         {
  1017.                             if(SerWriteBypass)
  1018.                             {
  1019.                                     /* Process the data... */
  1020.  
  1021.                                 while(Size = TranslateBuffer(&Handle))
  1022.                                 {
  1023.                                     if((*SerWriteBypass)(TranslateData,Size))
  1024.                                     {
  1025.                                             /* ...and send it. */
  1026.  
  1027.                                         DoSerialWrite(TranslateData,Size);
  1028.                                     }
  1029.  
  1030.                                     BytesOut += Size;
  1031.                                 }
  1032.                             }
  1033.                             else
  1034.                             {
  1035.                                     /* Process the data... */
  1036.  
  1037.                                 while(Size = TranslateBuffer(&Handle))
  1038.                                 {
  1039.                                         /* ...and send it. */
  1040.  
  1041.                                     DoSerialWrite(TranslateData,Size);
  1042.  
  1043.                                     BytesOut += Size;
  1044.                                 }
  1045.                             }
  1046.                         }
  1047.                         else
  1048.                         {
  1049.                             if(Marking)
  1050.                                 DropMarker();
  1051.  
  1052.                             if(SerWriteBypass)
  1053.                             {
  1054.                                 while(Size = TranslateBuffer(&Handle))
  1055.                                 {
  1056.                                     if((*SerWriteBypass)(TranslateData,Size))
  1057.                                     {
  1058.                                             /* Process the data while it is transferred. */
  1059.  
  1060.                                         StartSerialWrite(TranslateData,Size);
  1061.  
  1062.                                         ConProcess(Buffer,Size);
  1063.  
  1064.                                         WaitSerialWrite();
  1065.                                     }
  1066.  
  1067.                                     BytesOut += Size;
  1068.                                 }
  1069.                             }
  1070.                             else
  1071.                             {
  1072.                                 while(Size = TranslateBuffer(&Handle))
  1073.                                 {
  1074.                                         /* Process the data while it is transferred. */
  1075.  
  1076.                                     StartSerialWrite(TranslateData,Size);
  1077.  
  1078.                                     ConProcess(TranslateData,Size);
  1079.  
  1080.                                     WaitSerialWrite();
  1081.  
  1082.                                     BytesOut += Size;
  1083.                                 }
  1084.                             }
  1085.                         }
  1086.                     }
  1087.                     else
  1088.                     {
  1089.                         if(SerWriteBypass)
  1090.                         {
  1091.                             if((*SerWriteBypass)(Buffer,Size))
  1092.                             {
  1093.                                     /* If full duplex is enabled, send the entire
  1094.                                      * buffer without processing.
  1095.                                      */
  1096.  
  1097.                                 if(Config -> SerialConfig -> Duplex == DUPLEX_FULL)
  1098.                                     DoSerialWrite(Buffer,Size);
  1099.                                 else
  1100.                                 {
  1101.                                         /* Process the data while it is transferred. */
  1102.  
  1103.                                     StartSerialWrite(Buffer,Size);
  1104.  
  1105.                                     if(Marking)
  1106.                                         DropMarker();
  1107.  
  1108.                                     ConProcess(Buffer,Size);
  1109.  
  1110.                                     WaitSerialWrite();
  1111.                                 }
  1112.                             }
  1113.                             else
  1114.                             {
  1115.                                 if(Config -> SerialConfig -> Duplex != DUPLEX_FULL)
  1116.                                 {
  1117.                                     if(Marking)
  1118.                                         DropMarker();
  1119.  
  1120.                                     ConProcess(Buffer,Size);
  1121.                                 }
  1122.                             }
  1123.                         }
  1124.                         else
  1125.                         {
  1126.                                 /* If full duplex is enabled, send the entire
  1127.                                  * buffer without processing.
  1128.                                  */
  1129.  
  1130.                             if(Config -> SerialConfig -> Duplex == DUPLEX_FULL)
  1131.                                 DoSerialWrite(Buffer,Size);
  1132.                             else
  1133.                             {
  1134.                                     /* Process the data while it is transferred. */
  1135.  
  1136.                                 StartSerialWrite(Buffer,Size);
  1137.  
  1138.                                 if(Marking)
  1139.                                     DropMarker();
  1140.  
  1141.                                 ConProcess(Buffer,Size);
  1142.  
  1143.                                 WaitSerialWrite();
  1144.                             }
  1145.                         }
  1146.  
  1147.                         BytesOut += Size;
  1148.                     }
  1149.                 }
  1150.             }
  1151.             else
  1152.             {
  1153.                 if(SendTable)
  1154.                 {
  1155.                     struct TranslationHandle Handle;
  1156.  
  1157.                         /* Set up for buffer translation. */
  1158.  
  1159.                     TranslateSetup(&Handle,Buffer,Size,TranslateData,512,SendTable);
  1160.  
  1161.                         /* Full or half duplex? */
  1162.  
  1163.                     if(Config -> SerialConfig -> Duplex == DUPLEX_FULL)
  1164.                     {
  1165.                         if(SerWriteBypass)
  1166.                         {
  1167.                                 /* Process the data... */
  1168.  
  1169.                             while(Size = TranslateBuffer(&Handle))
  1170.                             {
  1171.                                 if((*SerWriteBypass)(TranslateData,Size))
  1172.                                 {
  1173.                                         /* ...and send it. */
  1174.  
  1175.                                     DoSerialWrite(TranslateData,Size);
  1176.                                 }
  1177.  
  1178.                                 BytesOut += Size;
  1179.                             }
  1180.                         }
  1181.                         else
  1182.                         {
  1183.                                 /* Process the data... */
  1184.  
  1185.                             while(Size = TranslateBuffer(&Handle))
  1186.                             {
  1187.                                     /* ...and send it. */
  1188.  
  1189.                                 DoSerialWrite(TranslateData,Size);
  1190.  
  1191.                                 BytesOut += Size;
  1192.                             }
  1193.                         }
  1194.                     }
  1195.                     else
  1196.                     {
  1197.                         if(Marking)
  1198.                             DropMarker();
  1199.  
  1200.                         if(SerWriteBypass)
  1201.                         {
  1202.                             while(Size = TranslateBuffer(&Handle))
  1203.                             {
  1204.                                 if((*SerWriteBypass)(TranslateData,Size))
  1205.                                 {
  1206.                                         /* Process the data while it is transferred. */
  1207.  
  1208.                                     StartSerialWrite(TranslateData,Size);
  1209.  
  1210.                                     ConProcess(TranslateData,Size);
  1211.  
  1212.                                     WaitSerialWrite();
  1213.                                 }
  1214.  
  1215.                                 BytesOut += Size;
  1216.                             }
  1217.                         }
  1218.                         else
  1219.                         {
  1220.                             while(Size = TranslateBuffer(&Handle))
  1221.                             {
  1222.                                     /* Process the data while it is transferred. */
  1223.  
  1224.                                 StartSerialWrite(TranslateData,Size);
  1225.  
  1226.                                 ConProcess(TranslateData,Size);
  1227.  
  1228.                                 WaitSerialWrite();
  1229.  
  1230.                                 BytesOut += Size;
  1231.                             }
  1232.                         }
  1233.                     }
  1234.                 }
  1235.                 else
  1236.                 {
  1237.                     if(SerWriteBypass)
  1238.                     {
  1239.                         if((*SerWriteBypass)(Buffer,Size))
  1240.                         {
  1241.                                 /* If full duplex is enabled, send the entire
  1242.                                  * buffer without processing.
  1243.                                  */
  1244.  
  1245.                             if(Config -> SerialConfig -> Duplex == DUPLEX_FULL)
  1246.                                 DoSerialWrite(Buffer,Size);
  1247.                             else
  1248.                             {
  1249.                                     /* Process the data while it is transferred. */
  1250.  
  1251.                                 StartSerialWrite(Buffer,Size);
  1252.  
  1253.                                 if(Marking)
  1254.                                     DropMarker();
  1255.  
  1256.                                 ConProcess(Buffer,Size);
  1257.  
  1258.                                 WaitSerialWrite();
  1259.                             }
  1260.                         }
  1261.                     }
  1262.                     else
  1263.                     {
  1264.                             /* If full duplex is enabled, send the entire
  1265.                              * buffer without processing.
  1266.                              */
  1267.  
  1268.                         if(Config -> SerialConfig -> Duplex == DUPLEX_FULL)
  1269.                             DoSerialWrite(Buffer,Size);
  1270.                         else
  1271.                         {
  1272.                                 /* Process the data while it is transferred. */
  1273.  
  1274.                             StartSerialWrite(Buffer,Size);
  1275.  
  1276.                             if(Marking)
  1277.                                 DropMarker();
  1278.  
  1279.                             ConProcess(Buffer,Size);
  1280.  
  1281.                             WaitSerialWrite();
  1282.                         }
  1283.                     }
  1284.  
  1285.                     BytesOut += Size;
  1286.                 }
  1287.             }
  1288.         }
  1289.     }
  1290. }
  1291.  
  1292.     /* RestartSerial():
  1293.      *
  1294.      *    Restart read/write activity on the serial line.
  1295.      */
  1296.  
  1297. VOID
  1298. RestartSerial()
  1299. {
  1300.     StartSerialRead(ReadBuffer,1);
  1301. }
  1302.  
  1303.     /* ClearSerial():
  1304.      *
  1305.      *    Terminate all read/write activity on the serial line.
  1306.      */
  1307.  
  1308. VOID
  1309. ClearSerial()
  1310. {
  1311.     StopSerialRead();
  1312.  
  1313.     DoSerialCmd(CMD_CLEAR);
  1314. }
  1315.  
  1316.     /* DeleteSerial():
  1317.      *
  1318.      *    Close the serial device and release all associated
  1319.      *    resources.
  1320.      */
  1321.  
  1322. VOID
  1323. DeleteSerial()
  1324. {
  1325.     BYTE Closed = FALSE;
  1326.  
  1327.     if(ReadRequest)
  1328.     {
  1329.         if(ReadRequest -> IOSer . io_Device)
  1330.         {
  1331.             struct Device    *Device;
  1332.             UBYTE             Name[MAX_FILENAME_LENGTH];
  1333.  
  1334.             StopSerialRead();
  1335.  
  1336.             if(!Config -> SerialConfig -> Shared)
  1337.             {
  1338.                 ReadRequest -> IOSer . io_Command = CMD_RESET;
  1339.  
  1340.                 DoIO(ReadRequest);
  1341.             }
  1342.  
  1343.             strcpy(Name,ReadRequest -> IOSer . io_Device -> dd_Library . lib_Node . ln_Name);
  1344.  
  1345.             CloseDevice(ReadRequest);
  1346.  
  1347.             Forbid();
  1348.  
  1349.             if(Device = (struct Device *)FindName(&SysBase -> DeviceList,Name))
  1350.                 RemDevice(Device);
  1351.  
  1352.             Permit();
  1353.  
  1354.             Closed = TRUE;
  1355.         }
  1356.  
  1357.         DeleteIORequest(ReadRequest);
  1358.  
  1359.         ReadRequest = NULL;
  1360.     }
  1361.  
  1362.     if(WriteRequest)
  1363.     {
  1364.         if(WriteRequest -> IOSer . io_Device && !Closed)
  1365.         {
  1366.             struct Device    *Device;
  1367.             UBYTE             Name[MAX_FILENAME_LENGTH];
  1368.  
  1369.             StopSerialWrite();
  1370.  
  1371.             strcpy(Name,WriteRequest -> IOSer . io_Device -> dd_Library . lib_Node . ln_Name);
  1372.  
  1373.             CloseDevice(WriteRequest);
  1374.  
  1375.             Forbid();
  1376.  
  1377.             if(Device = (struct Device *)FindName(&SysBase -> DeviceList,Name))
  1378.                 RemDevice(Device);
  1379.  
  1380.             Permit();
  1381.         }
  1382.  
  1383.         if(WriteRequest -> IOSer . io_Message . mn_ReplyPort)
  1384.             DeleteMsgPort(WriteRequest -> IOSer . io_Message . mn_ReplyPort);
  1385.  
  1386.         DeleteIORequest(WriteRequest);
  1387.  
  1388.         WriteRequest = NULL;
  1389.     }
  1390.  
  1391.     if(ReadPort)
  1392.     {
  1393.         DeleteMsgPort(ReadPort);
  1394.  
  1395.         ReadPort = NULL;
  1396.     }
  1397.  
  1398.     if(ReadBuffer)
  1399.     {
  1400.         FreeVecPooled(ReadBuffer);
  1401.  
  1402.         ReadBuffer = NULL;
  1403.     }
  1404.  
  1405.     if(HostReadBuffer)
  1406.     {
  1407.         FreeVecPooled(HostReadBuffer);
  1408.  
  1409.         HostReadBuffer = NULL;
  1410.     }
  1411.  
  1412.     if(StripBuffer)
  1413.     {
  1414.         FreeVecPooled(StripBuffer);
  1415.  
  1416.         StripBuffer = NULL;
  1417.     }
  1418.  
  1419.     if(OwnDevUnitBase && SerialDevice[0] && UnitNumber != -1 && SerialLocked)
  1420.     {
  1421.         FreeDevUnit(SerialDevice,UnitNumber);
  1422.  
  1423.         SerialDevice[0] = 0;
  1424.  
  1425.         UnitNumber = -1;
  1426.     }
  1427.  
  1428.     if(OwnDevBit != -1)
  1429.     {
  1430.         FreeSignal(OwnDevBit);
  1431.  
  1432.         OwnDevBit = -1;
  1433.     }
  1434.  
  1435.     SerialLocked = FALSE;
  1436.  
  1437.     ResetSerialRead();
  1438.     ResetSerialWrite();
  1439. }
  1440.  
  1441.     /* GetSerialError(LONG Error,BYTE *Reset):
  1442.      *
  1443.      *    Return an error message for each possible serial device error.
  1444.      */
  1445.  
  1446. STRPTR __regargs
  1447. GetSerialError(LONG Error,BYTE *Reset)
  1448. {
  1449.     if(Reset)
  1450.         *Reset = FALSE;
  1451.  
  1452.     switch(Error)
  1453.     {
  1454.         case IOERR_BADLENGTH:
  1455.         case IOERR_BADADDRESS:
  1456.         case IOERR_SELFTEST:
  1457.         case IOERR_OPENFAIL:
  1458.  
  1459.             return(LocaleString(MSG_SERIAL_OPENFAILURE_TXT));
  1460.  
  1461.         case SerErr_DevBusy:
  1462.  
  1463.             return(LocaleString(MSG_SERIAL_ERROR_DEVBUSY_TXT));
  1464.  
  1465.         case SerErr_BaudMismatch:
  1466.  
  1467.             if(Reset)
  1468.                 *Reset = TRUE;
  1469.  
  1470.             return(LocaleString(MSG_SERIAL_ERROR_BAUDMISMATCH_TXT));
  1471.  
  1472.         case SerErr_BufErr:
  1473.  
  1474.             if(Reset)
  1475.                 *Reset = TRUE;
  1476.  
  1477.             return(LocaleString(MSG_SERIAL_ERROR_BUFERR_TXT));
  1478.  
  1479.         case SerErr_InvParam:
  1480.  
  1481.             if(Reset)
  1482.                 *Reset = TRUE;
  1483.  
  1484.             return(LocaleString(MSG_SERIAL_ERROR_INVPARAM_TXT));
  1485.  
  1486.         case SerErr_LineErr:
  1487.  
  1488.             return(LocaleString(MSG_SERIAL_ERROR_LINEERR_TXT));
  1489.  
  1490.         case SerErr_ParityErr:
  1491.  
  1492.             if(Reset)
  1493.                 *Reset = TRUE;
  1494.  
  1495.             return(LocaleString(MSG_SERIAL_ERROR_PARITYERR_TXT));
  1496.  
  1497.         case SerErr_TimerErr:
  1498.  
  1499.             return(LocaleString(MSG_SERIAL_ERROR_TIMERERR_TXT));
  1500.  
  1501.         case SerErr_BufOverflow:
  1502.  
  1503.             if(Reset)
  1504.                 *Reset = TRUE;
  1505.  
  1506.             return(LocaleString(MSG_SERIAL_ERROR_BUFOVERFLOW_TXT));
  1507.  
  1508.         case SerErr_NoDSR:
  1509.  
  1510.             return(LocaleString(MSG_SERIAL_ERROR_NODSR_TXT));
  1511.  
  1512.     /*    case SerErr_UnitBusy:    */
  1513.         case IOERR_UNITBUSY:
  1514.         case 16:
  1515.  
  1516.             return(LocaleString(MSG_SERIAL_ERROR_UNIT_BUSY_TXT));
  1517.  
  1518.         default:
  1519.  
  1520.             return(NULL);
  1521.     }
  1522. }
  1523.  
  1524.     /* CreateSerial():
  1525.      *
  1526.      *    Create handles for the serial device and open it.
  1527.      */
  1528.  
  1529. STRPTR
  1530. CreateSerial()
  1531. {
  1532.     struct MsgPort *WritePort;
  1533.  
  1534.         /* If OwnDevUnit.library is available, try to lock
  1535.          * the serial driver.
  1536.          */
  1537.  
  1538.     if(OwnDevUnitBase && Config -> SerialConfig -> UseOwnDevUnit && !(Config -> SerialConfig -> Shared && Config -> SerialConfig -> NoODUIfShared))
  1539.     {
  1540.         STRPTR Error;
  1541.  
  1542.             /* Allocate a signal for OwnDevUnit, in case we might need it. */
  1543.  
  1544.         if(OwnDevBit == -1)
  1545.         {
  1546.             OwnDevBit = AllocSignal(-1);
  1547.  
  1548.                 /* Attempt to lock the device unit... */
  1549.  
  1550.             Error = AttemptDevUnit(Config -> SerialConfig -> SerialDevice,Config -> SerialConfig -> UnitNumber,TermIDString,OwnDevBit == -1 ? NULL : OwnDevBit);
  1551.         }
  1552.         else
  1553.             Error = NULL;
  1554.  
  1555.         if(Error)
  1556.         {
  1557.                 /* Check for error type if any. */
  1558.  
  1559.             if(!Strnicmp(Error,ODUERR_LEADCHAR,1))
  1560.                 SPrintf(SharedBuffer,LocaleString(MSG_SERIAL_ERROR_ACCESSING_TXT),Config -> SerialConfig -> SerialDevice,Config -> SerialConfig -> UnitNumber,&Error[1]);
  1561.             else
  1562.                 SPrintf(SharedBuffer,LocaleString(MSG_SERIAL_DEVICE_IN_USE_TXT),Config -> SerialConfig -> SerialDevice,Config -> SerialConfig -> UnitNumber,Error);
  1563.  
  1564.             SerialDevice[0] = 0;
  1565.  
  1566.             UnitNumber = -1;
  1567.  
  1568.             SerialLocked = FALSE;
  1569.  
  1570.             return(SharedBuffer);
  1571.         }
  1572.         else
  1573.         {
  1574.             strcpy(SerialDevice,Config -> SerialConfig -> SerialDevice);
  1575.  
  1576.             UnitNumber = Config -> SerialConfig -> UnitNumber;
  1577.  
  1578.             SerialLocked = TRUE;
  1579.         }
  1580.     }
  1581.     else
  1582.         SerialLocked = FALSE;
  1583.  
  1584.     Update_CR_LF_Translation();
  1585.  
  1586.     if(ReadBuffer = AllocVecPooled(Config -> SerialConfig -> SerialBufferSize,MEMF_ANY))
  1587.     {
  1588.         if(StripBuffer = AllocVecPooled(Config -> SerialConfig -> SerialBufferSize,MEMF_ANY))
  1589.         {
  1590.             if(XProtocolBase && (TransferBits & XPRS_HOSTNOWAIT))
  1591.                 HostReadBuffer = AllocVecPooled(Config -> SerialConfig -> SerialBufferSize,MEMF_ANY);
  1592.             else
  1593.                 HostReadBuffer = NULL;
  1594.  
  1595.             if(ReadPort = (struct MsgPort *)CreateMsgPort())
  1596.             {
  1597.                 if(ReadRequest = (struct IOExtSer *)CreateIORequest(ReadPort,sizeof(struct IOExtSer)))
  1598.                 {
  1599.                     if(WritePort = (struct MsgPort *)CreateMsgPort())
  1600.                     {
  1601.                         if(WriteRequest = (struct IOExtSer *)CreateIORequest(WritePort,sizeof(struct IOExtSer)))
  1602.                         {
  1603.                             BYTE HandshakingProtocol = Config -> SerialConfig -> HandshakingProtocol;
  1604.                             LONG Error;
  1605.  
  1606.                                 /* In order to avoid trouble, the device driver is to be opened in
  1607.                                  * plain mode, i.e. no RTS/CTS handshaking will be enabled.
  1608.                                  */
  1609.  
  1610.                             if(Config -> SerialConfig -> HandshakingProtocol == HANDSHAKING_RTSCTS_DSR)
  1611.                                 HandshakingProtocol = HANDSHAKING_RTSCTS;
  1612.  
  1613.                             UseRTS_CTS = TRUE;
  1614.  
  1615. Reset:                        SetSerialReadAttributes(
  1616.                                 SERA_Baud,            Config -> SerialConfig -> BaudRate,
  1617.                                 SERA_BreakTime,        Config -> SerialConfig -> BreakLength,
  1618.                                 SERA_BitsPerChar,    Config -> SerialConfig -> BitsPerChar,
  1619.                                 SERA_StopBits,        Config -> SerialConfig -> StopBits,
  1620.                                 SERA_BufferSize,    Config -> SerialConfig -> SerialBufferSize,
  1621.                                 SERA_Parity,        Config -> SerialConfig -> Parity,
  1622.                                 SERA_Handshaking,    HandshakingProtocol,
  1623.                                 SERA_HighSpeed,        Config -> SerialConfig -> HighSpeed,
  1624.                                 SERA_Shared,        Config -> SerialConfig -> Shared,
  1625.                             TAG_DONE);
  1626.  
  1627.                             if(!(Error = OpenDevice(Config -> SerialConfig -> SerialDevice,Config -> SerialConfig -> UnitNumber,ReadRequest,0)))
  1628.                             {
  1629.                                 SetSerialReadAttributes(SERA_Handshaking,HandshakingProtocol,TAG_DONE);
  1630.  
  1631.                                 CopyMem(ReadRequest,WriteRequest,sizeof(struct IOExtSer));
  1632.  
  1633.                                 WriteRequest -> IOSer . io_Message . mn_ReplyPort = WritePort;
  1634.  
  1635.                                 if(Config -> SerialConfig -> HandshakingProtocol == HANDSHAKING_RTSCTS_DSR && HandshakingProtocol == HANDSHAKING_RTSCTS)
  1636.                                 {
  1637.                                         /* Now check if the DSR signal is enabled. */
  1638.  
  1639.                                     if(GetSerialStatus() & CIAF_COMDSR)
  1640.                                     {
  1641.                                             /* If this is the first time the driver gets initialized,
  1642.                                              * skip the notification message and just drop the RTS/CTS
  1643.                                              * handshaking bit.
  1644.                                              */
  1645.  
  1646.                                         if(FirstInvocation)
  1647.                                             UseRTS_CTS = FALSE;
  1648.                                         else
  1649.                                         {
  1650.                                             do
  1651.                                             {
  1652.                                                 if(!MyEasyRequest(Window,LocaleString(MSG_SERIAL_NO_DSR_SIGNAL_TXT),LocaleString(MSG_SERIAL_RETRY_CANCEL_TXT)))
  1653.                                                 {
  1654.                                                     UseRTS_CTS = FALSE;
  1655.                                                     break;
  1656.                                                 }
  1657.                                             }
  1658.                                             while(GetSerialStatus() & CIAF_COMDSR);
  1659.                                         }
  1660.                                     }
  1661.  
  1662.                                     if(!UseRTS_CTS)
  1663.                                     {
  1664.                                         CloseDevice(ReadRequest);
  1665.  
  1666.                                         ReadRequest -> IOSer . io_Device = NULL;
  1667.  
  1668.                                         HandshakingProtocol = HANDSHAKING_NONE;
  1669.  
  1670.                                         goto Reset;
  1671.                                     }
  1672.                                 }
  1673.  
  1674.                                 ResetSerialRead();
  1675.                                 ResetSerialWrite();
  1676.  
  1677.                                 SetFlags();
  1678.  
  1679.                                 RestartSerial();
  1680.  
  1681.                                 return(NULL);
  1682.                             }
  1683.                             else
  1684.                             {
  1685.                                 STRPTR String;
  1686.  
  1687.                                 ReadRequest -> IOSer . io_Device = NULL;
  1688.  
  1689.                                 DeleteSerial();
  1690.  
  1691.                                 SerialMessage = NULL;
  1692.  
  1693.                                 if(!(String = GetSerialError(Error,NULL)))
  1694.                                     String = LocaleString(MSG_SERIAL_ERROR_DEVBUSY_TXT);
  1695.  
  1696.                                 SPrintf(SharedBuffer,String,Config -> SerialConfig -> SerialDevice,Config -> SerialConfig -> UnitNumber);
  1697.  
  1698.                                 return(SharedBuffer);
  1699.                             }
  1700.                         }
  1701.                     }
  1702.                     else
  1703.                     {
  1704.                         SerialMessage = NULL;
  1705.  
  1706.                         DeleteSerial();
  1707.  
  1708.                         return(LocaleString(MSG_SERIAL_FAILED_TO_CREATE_WRITE_PORT_TXT));
  1709.                     }
  1710.                 }
  1711.             }
  1712.             else
  1713.             {
  1714.                 SerialMessage = NULL;
  1715.  
  1716.                 DeleteSerial();
  1717.  
  1718.                 return(LocaleString(MSG_SERIAL_FAILED_TO_CREATE_READ_PORT_TXT));
  1719.             }
  1720.         }
  1721.     }
  1722.  
  1723.     SerialMessage = NULL;
  1724.  
  1725.     DeleteSerial();
  1726.  
  1727.     return(LocaleString(MSG_SERIAL_NOT_ENOUGH_MEMORY_TXT));
  1728. }
  1729.  
  1730.     /* ReconfigureSerial(struct Window *Window,struct SerialSettings *SerialConfig):
  1731.      *
  1732.      *    Reconfigure the serial driver according to the new
  1733.      *    serial settings.
  1734.      */
  1735.  
  1736. BYTE __regargs
  1737. ReconfigureSerial(struct Window *Window,struct SerialSettings *SerialConfig)
  1738. {
  1739.     BYTE    Success    = RECONFIGURE_NOCHANGE,
  1740.             IsNew;
  1741.  
  1742.         /* Are new settings to be installed or have they already
  1743.          * been copied to the correct locations?
  1744.          */
  1745.  
  1746.     if(SerialConfig)
  1747.     {
  1748.         if(memcmp(Config -> SerialConfig,SerialConfig,sizeof(struct SerialSettings)))
  1749.             IsNew = TRUE;
  1750.         else
  1751.             IsNew = FALSE;
  1752.     }
  1753.     else
  1754.     {
  1755.         if(memcmp(Config -> SerialConfig,PrivateConfig -> SerialConfig,sizeof(struct SerialSettings)))
  1756.             IsNew = TRUE;
  1757.         else
  1758.             IsNew = FALSE;
  1759.     }
  1760.  
  1761.         /* Any changes in the serial configuration area? */
  1762.  
  1763.     if(IsNew)
  1764.     {
  1765.         BYTE SameDevice = TRUE;
  1766.  
  1767.             /* Any new data to swap in? */
  1768.  
  1769.         if(SerialConfig)
  1770.         {
  1771.                 /* Store the previous settings. */
  1772.  
  1773.             SaveConfig(Config,PrivateConfig);
  1774.  
  1775.                 /* Install the new settings. */
  1776.  
  1777.             memcpy(Config -> SerialConfig,SerialConfig,sizeof(struct SerialSettings));
  1778.         }
  1779.  
  1780.             /* Any device name or unit number change? */
  1781.  
  1782.         if(strcmp(PrivateConfig -> SerialConfig -> SerialDevice,Config -> SerialConfig -> SerialDevice) || PrivateConfig -> SerialConfig -> UnitNumber != Config -> SerialConfig -> UnitNumber || PrivateConfig -> SerialConfig -> UseOwnDevUnit != Config -> SerialConfig -> UseOwnDevUnit)
  1783.             SameDevice = FALSE;
  1784.         else
  1785.         {
  1786.                 /* Handshaking mode changed to RTS/CTS protocol? */
  1787.  
  1788.             if((PrivateConfig -> SerialConfig -> HandshakingProtocol == HANDSHAKING_NONE && Config -> SerialConfig -> HandshakingProtocol != HANDSHAKING_NONE) || (PrivateConfig -> SerialConfig -> HandshakingProtocol != HANDSHAKING_NONE && Config -> SerialConfig -> HandshakingProtocol == HANDSHAKING_NONE))
  1789.                 SameDevice = FALSE;
  1790.             else
  1791.             {
  1792.                     // Did the ODU options change?
  1793.  
  1794.                 if(PrivateConfig -> SerialConfig -> NoODUIfShared != Config -> SerialConfig -> NoODUIfShared && Config -> SerialConfig -> Shared)
  1795.                     SameDevice = FALSE;
  1796.             }
  1797.         }
  1798.  
  1799.             /* Stop any IO activity. */
  1800.  
  1801.         if(ReadRequest)
  1802.             ClearSerial();
  1803.         else
  1804.             SameDevice = FALSE;
  1805.  
  1806.             /* No dramatic changes? Simply change the parameters. */
  1807.  
  1808.         if(SameDevice)
  1809.         {
  1810.             LONG Error;
  1811.  
  1812.             if(PrivateConfig -> SerialConfig -> SerialBufferSize != Config -> SerialConfig -> SerialBufferSize)
  1813.             {
  1814.                 STRPTR NewReadBuffer;
  1815.  
  1816.                 if(NewReadBuffer = AllocVecPooled(Config -> SerialConfig -> SerialBufferSize,MEMF_ANY))
  1817.                 {
  1818.                     STRPTR NewStripBuffer;
  1819.  
  1820.                     if(NewStripBuffer = AllocVecPooled(Config -> SerialConfig -> SerialBufferSize,MEMF_ANY))
  1821.                     {
  1822.                         FreeVecPooled(ReadBuffer);
  1823.                         FreeVecPooled(StripBuffer);
  1824.  
  1825.                         ReadBuffer    = NewReadBuffer;
  1826.                         StripBuffer    = NewStripBuffer;
  1827.                     }
  1828.                     else
  1829.                     {
  1830.                         FreeVecPooled(NewReadBuffer);
  1831.  
  1832.                         Config -> SerialConfig -> SerialBufferSize = PrivateConfig -> SerialConfig -> SerialBufferSize;
  1833.                     }
  1834.                 }
  1835.                 else
  1836.                     Config -> SerialConfig -> SerialBufferSize = PrivateConfig -> SerialConfig -> SerialBufferSize;
  1837.             }
  1838.  
  1839.             Update_CR_LF_Translation();
  1840.  
  1841.                 /* Use new parameters... */
  1842.  
  1843.             Error = SetFlags();
  1844.  
  1845.                 /* Trouble? */
  1846.  
  1847.             if(Error)
  1848.             {
  1849.                 STRPTR    String;
  1850.                 BYTE    Reset;
  1851.  
  1852.                     /* Query the error message. */
  1853.  
  1854.                 if(!(String = GetSerialError(Error,&Reset)))
  1855.                     String = LocaleString(MSG_SERIAL_ERROR_DEVBUSY_TXT);
  1856.  
  1857.                     /* Build the device name/unit number string. */
  1858.  
  1859.                 SPrintf(SharedBuffer,String,Config -> SerialConfig -> SerialDevice,Config -> SerialConfig -> UnitNumber);
  1860.  
  1861.                     /* Display the error requester. */
  1862.  
  1863.                 LT_LockWindow(Window);
  1864.  
  1865.                 MyEasyRequest(Window,LocaleString(MSG_GLOBAL_TERM_HAS_A_PROBLEM_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT),SharedBuffer);
  1866.  
  1867.                 LT_UnlockWindow(Window);
  1868.  
  1869.                     /* Is a serial driver reset required? */
  1870.  
  1871.                 if(Reset)
  1872.                 {
  1873.                         /* Execute the reset command. */
  1874.  
  1875.                     DoSerialCmd(CMD_RESET);
  1876.  
  1877.                         /* Copy the serial driver settings
  1878.                          * to the global configuration.
  1879.                          */
  1880.  
  1881.                     CopyWriteFlags();
  1882.  
  1883.                         /* Also set the read request driver
  1884.                          * flags.
  1885.                          */
  1886.  
  1887.                     SetFlags();
  1888.                 }
  1889.             }
  1890.  
  1891.                 /* Restart read request. */
  1892.  
  1893.             RestartSerial();
  1894.         }
  1895.         else
  1896.         {
  1897.             STRPTR Error;
  1898.  
  1899.                 /* Shut down the serial driver. */
  1900.  
  1901.             DeleteSerial();
  1902.  
  1903.                 /* Reinitialize the serial driver. */
  1904.  
  1905.             if(!(Error = CreateSerial()))
  1906.             {
  1907.                     /* Free the work buffer. */
  1908.  
  1909.                 if(StripBuffer)
  1910.                     FreeVecPooled(StripBuffer);
  1911.  
  1912.                     /* Try to allocate a new serial data work buffer. */
  1913.  
  1914.                 if(!(StripBuffer = (STRPTR)AllocVecPooled(Config -> SerialConfig -> SerialBufferSize,MEMF_ANY)))
  1915.                     Error = LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT);
  1916.             }
  1917.  
  1918.                 /* Trouble? */
  1919.  
  1920.             if(Error)
  1921.             {
  1922.                     /* Display the error requester. */
  1923.  
  1924.                 LT_LockWindow(Window);
  1925.  
  1926.                 MyEasyRequest(Window,LocaleString(MSG_GLOBAL_TERM_HAS_A_PROBLEM_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT),Error);
  1927.  
  1928.                     /* Clean up the mess :-( */
  1929.  
  1930.                 DeleteSerial();
  1931.  
  1932.                 LT_UnlockWindow(Window);
  1933.  
  1934.                 Success = RECONFIGURE_FAILURE;
  1935.             }
  1936.             else
  1937.             {
  1938.                     /* Are we to display an error message? */
  1939.  
  1940.                 if(SerialMessage)
  1941.                 {
  1942.                     MyEasyRequest(Window,LocaleString(MSG_GLOBAL_TERM_HAS_A_PROBLEM_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT),SerialMessage);
  1943.  
  1944.                     SerialMessage = NULL;
  1945.                 }
  1946.             }
  1947.         }
  1948.     }
  1949.  
  1950.     return(Success);
  1951. }
  1952.  
  1953.     /* ReopenSerial():
  1954.      *
  1955.      *    Reopen the serial driver.
  1956.      */
  1957.  
  1958. VOID
  1959. ReopenSerial()
  1960. {
  1961.     BYTE    SerialClosed = TRUE;
  1962.     STRPTR    Error;
  1963.  
  1964.     do
  1965.     {
  1966.         if(Error = CreateSerial())
  1967.         {
  1968.             DeleteSerial();
  1969.  
  1970.             switch(MyEasyRequest(Window,LocaleString(MSG_GLOBAL_TERM_HAS_A_PROBLEM_TXT),LocaleString(MSG_TERMMAIN_RETRY_IGNORE_QUIT_TXT),Error))
  1971.             {
  1972.                 case 2:    SerialClosed = FALSE;
  1973.                     break;
  1974.  
  1975.                 case 0:    MainTerminated = TRUE;
  1976.                     break;
  1977.             }
  1978.         }
  1979.         else
  1980.         {
  1981.             SerialClosed = FALSE;
  1982.  
  1983.             if(SerialMessage)
  1984.             {
  1985.                 MyEasyRequest(Window,LocaleString(MSG_GLOBAL_TERM_HAS_A_PROBLEM_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT),SerialMessage);
  1986.  
  1987.                 SerialMessage = NULL;
  1988.             }
  1989.         }
  1990.     }
  1991.     while(SerialClosed);
  1992. }
  1993.  
  1994.     /* HandleSerial():
  1995.      *
  1996.      *    Handle the data coming in from the serial line.
  1997.      */
  1998.  
  1999. BYTE
  2000. HandleSerial()
  2001. {
  2002.     BYTE MoreData = FALSE;
  2003.  
  2004.     if(ReadPort && !ReleaseSerial && Status != STATUS_HOLDING)
  2005.     {
  2006.         if(HostReadBuffer)
  2007.         {
  2008.             LONG Length = XProtocolHostMon(XprIO,HostReadBuffer,0,SerialBufferSize);
  2009.  
  2010.             if(Translate_CR_LF)
  2011.                 Length = (* Translate_CR_LF)(HostReadBuffer,Length);
  2012.  
  2013.             if(Length)
  2014.             {
  2015.                 if(Marking)
  2016.                     DropMarker();
  2017.  
  2018.                 ConProcess(HostReadBuffer,Length);
  2019.  
  2020.                 if(Status == STATUS_HOLDING)
  2021.                     return(FALSE);
  2022.             }
  2023.  
  2024.             MoreData = TRUE;
  2025.         }
  2026.  
  2027.         if(DataHold)
  2028.         {
  2029.             register STRPTR    Data    = DataHold;
  2030.             register LONG    Size    = DataSize;
  2031.  
  2032.             DataHold = NULL;
  2033.  
  2034.             if(Marking)
  2035.                 DropMarker();
  2036.  
  2037.             (* ConTransfer)(Data,Size);
  2038.  
  2039.             MoreData = TRUE;
  2040.  
  2041.             RestartSerial();
  2042.  
  2043.             return(MoreData);
  2044.         }
  2045.  
  2046.             /* Any news? */
  2047.  
  2048.         if(CheckSerialRead())
  2049.         {
  2050.             MoreData = TRUE;
  2051.  
  2052.             if(!WaitSerialRead())
  2053.             {
  2054.                 if(Marking)
  2055.                     DropMarker();
  2056.  
  2057.                 BytesIn++;
  2058.  
  2059.                 if(Translate_CR_LF)
  2060.                 {
  2061.                     register LONG Size = (* Translate_CR_LF)(ReadBuffer,1);
  2062.  
  2063.                     if(Size)
  2064.                         (* ConTransfer)(ReadBuffer,Size);
  2065.                 }
  2066.                 else
  2067.                     (* ConTransfer)(ReadBuffer,1);
  2068.  
  2069.                 if(Status != STATUS_HOLDING && !DataHold)
  2070.                 {
  2071.                     ULONG Length;
  2072.  
  2073.                         /* Check how many bytes are still in
  2074.                          * the serial buffer.
  2075.                          */
  2076.  
  2077.                     if(Length = GetSerialWaiting())
  2078.                     {
  2079.                         ULONG Max = SerialBufferSize;
  2080.  
  2081.                         if(Max > Config -> SerialConfig -> Quantum)
  2082.                             Max = Config -> SerialConfig -> Quantum;
  2083.  
  2084.                         if(Length > Max)
  2085.                             Length = Max;
  2086.  
  2087.                         if(!DoSerialRead(ReadBuffer,Length))
  2088.                         {
  2089.                             BytesIn += Length;
  2090.  
  2091.                             if(Translate_CR_LF)
  2092.                             {
  2093.                                 if(Length = (* Translate_CR_LF)(ReadBuffer,Length))
  2094.                                     (* ConTransfer)(ReadBuffer,Length);
  2095.                             }
  2096.                             else
  2097.                                 (* ConTransfer)(ReadBuffer,Length);
  2098.                         }
  2099.                     }
  2100.                 }
  2101.             }
  2102.  
  2103.             if(DataHold)
  2104.                 ClrSignal(SIG_SERIAL);
  2105.             else
  2106.                 RestartSerial();
  2107.         }
  2108.     }
  2109.  
  2110.     return(MoreData);
  2111. }
  2112.